package Class_test3;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class MyReflect {
    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        //读取配置文件中的信息
        Properties prop = new Properties();
        FileInputStream fis = new FileInputStream("D:\\Idea_xiangmu\\IdeaProjects\\Reflect_fanshe\\prop.properties");
        prop.load(fis);
        System.out.println(prop);

        //获取配置文件中的信息
        String className = prop.getProperty("class");
        String methodName = prop.getProperty("method");
        System.out.println(className);
        System.out.println(methodName);

        //创建对象
        Class clazz = Class.forName(className);
        //获取构造方法
        Constructor con=clazz.getDeclaredConstructor();
        Object o = con.newInstance();
        System.out.println(o);

        //获取属性
        Field field = clazz.getDeclaredField("name");
        //设置属性为可访问
        field.setAccessible(true);
        //设置属性值
        field.set(o,"张三");
        //获取属性值
        System.out.println(field.get(o));

        //获取方法
        Method method = clazz.getDeclaredMethod(methodName);
        //设置方法为可访问
        method.setAccessible(true);
        //调用方法
        method.invoke(o);
    }
}
